package com.sudo.boot.service.sys.jobservice.mail;

import com.sudo.boot.common.constant.enums.SysJobAction;
import com.sudo.boot.email.pojo.MailInfo;
import com.sudo.boot.mapper.sys.entity.SysJobLog;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

/**
 * 构建邮件模板
 */
@Component
public class MailInfoUtil {

    private MailInfoUtil(){}

    /**
     * 注意： 静态成员变量不能@Resource直接属性注入，所以可以采用构造注入
     */


    // 任务调度日志邮件（如果失败，则发邮件提醒）
    public static MailInfo methodJobExceptionMail(String mailAddress, SysJobLog sysJobLog){
        MailInfo mailInfo = new MailInfo();
        String sysJobAction = SysJobAction.valueOf(sysJobLog.getJobAction()).getDesc();
        String title = "任务【" + sysJobLog.getJobId() + "】["+ sysJobAction +"]运行失败：" + sysJobLog.getJobNote();
        mailInfo.setSubject(title);
        StringBuilder text = new StringBuilder();
        if (StringUtils.hasText(sysJobLog.getJobServer())) {
            text.append("【节点】：").append(sysJobLog.getJobServer()).append("<br/>");
        }else {
            text.append("【节点】：本地服务").append("<br/>");
        }
        text.append("【任务】：").append(sysJobLog.getJobId()).append("<br/>");
        text.append("【模块】：").append( sysJobLog.getJobBean()).append("<br/>");
        text.append("【方法】：").append(sysJobLog.getJobMethod()).append("<br/>");
        if(StringUtils.hasText(sysJobLog.getJobParams())){
            text.append("【参数】：").append(sysJobLog.getJobParams()).append("<br/>");
        }else {
            text.append("【参数】：无").append("<br/>");
        }
        text.append("【耗时】：").append(sysJobLog.getJobUseTime()).append("ms").append("<br/>");
        text.append("【运行时间】：").append(sysJobLog.getJobStartTime()).append("<br/>");
        text.append("【结束时间】：").append(sysJobLog.getJobEndTime()).append("<br/>");
        text.append("【异常说明】：").append(sysJobLog.getJobNote()).append("<br/>");
        text.append("【异常详情】：").append(sysJobLog.getJobNoteDesc()).append("<br/>");

        mailInfo.setContent(text.toString());
        String[] mailSendArr = mailAddress.split(",");
        mailInfo.setReceiver(mailSendArr);
        mailInfo.setCc(mailSendArr);
        return mailInfo;
    }

    // 用户注册，邮箱验证码
    public static MailInfo emailVerifyCode(String emailAddress, String code) {
        MailInfo mailInfo = new MailInfo();
        mailInfo.setSubject("JBuss验证码");
        mailInfo.setContent("欢迎使用JBuss，您的验证码是：" + code);
        String[] mailSendArr = emailAddress.split(",");
        mailInfo.setReceiver(mailSendArr);
        return mailInfo;
    }
}
